from time import sleep

from selenium.webdriver.common.by import By

import page
from base.base import Base
from tool.get_log import GetLogger

log = GetLogger().get_log()
class PageStudyManage(Base):

    def pagestudymanage_click_manage(self):
        self.base_click_element(page.wd_study_management)

    def pagestudymanage_click_my_course(self):
        self.base_click_element(page.studymanage_my_course)

    def pagestudymanage_if_my_course1_exist(self):
        return self.base_if_element_exist(page.studymanage_my_course1)

    def pagestudymanage_return_to_wdpage_keycode(self):
        for a in range(10):
            log.info(f'正在返回我的页面，第{a + 1}次尝试')
            if self.base_if_element_exist(page.wd_tab):
                break
            else:
                self.driver.press_keycode(4)

    # 添加一个课程
    def pagestudymanage_add_course1(self):
        self.pagestudymanage_return_to_wdpage_keycode()
        self.base_click_element(page.glsx_tab)
        try:
            self.base_click_element(page.glsx_tanchuang_close)
        except:
            pass
        self.base_click_element(page.studymanage_get_course1)
        self.base_click_element(page.studymanage_get_course1_close_tanchuang)
        self.base_click_element(page.wd_tab)
        self.base_click_element(page.wd_study_management)
        self.base_click_element(page.studymanage_my_course)

    def pagestudymanage_click_course1(self):
        self.base_click_element(page.studymanage_my_course1)

    def pagestudymanage_if_course1_success(self):
        return self.base_if_element_exist(page.studymanage_my_course_confirm)

    def pagestudymanage_click_pronounce(self):
        self.base_click_element(page.studymanage_pronounce)

    def pagestudymanage_click_3ci(self):
        self.base_click_element(page.studymanage_pronounce_3ci)

    def pagestudymanage_click_download(self):
        self.base_click_element(page.studymanage_download)

    def pagestudymanage_if_download1_exist(self):
        return self.base_if_element_exist(page.studymanage_download1)

    def pagestudymanage_click_download1(self):
        self.base_click_element(page.studymanage_download1)

    def pagestudymanage_add_download1(self):
        self.pagestudymanage_return_to_wdpage_keycode()
        self.base_click_element(page.wsy_tab)
        self.base_click_element(page.wsy_start_study)
        self.base_click_element(page.study_video)
        self.base_click_element(page.study_video_download)
        sleep(10)
        self.pagestudymanage_return_to_wdpage_keycode()
        self.base_click_element(page.wd_tab)
        self.pagestudymanage_click_manage()
        self.pagestudymanage_click_download()

    def pagestudymanage_click_my_collect(self):
        self.base_click_element(page.studymanage_my_collection)

    def pagestudymanage_if_my_collect_empty(self):
        return self.base_if_element_exist(page.studymanage_my_collection_empty)

    def pagestudymanage_add_my_collect_1(self):
        self.pagestudymanage_return_to_wdpage_keycode()
        self.base_click_element(page.discovery_tab)
        title = self.base_get_element_text(page.discovery_card_content_1_title)
        self.base_click_element(page.discovery_card_content_1)
        sleep(3)
        self.base_click_element(page.discovery_card_content_1_collect_btn)
        self.pagestudymanage_return_to_wdpage_keycode()
        self.base_click_element(page.wd_tab)
        self.pagestudymanage_click_manage()
        self.pagestudymanage_click_my_collect()
        return title

    def pagestudymanage_assert_add_collection(self,title):
        loc = By.XPATH,f"//android.widget.TextView[@text = '{title}']"
        return self.base_if_element_exist(loc)

if __name__ == '__main__':
    pass